package mx.com.crowdgine.camera;

import java.awt.Component;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.media.opengl.GL2;
import mx.com.crowdgine.entity.Entity;
import mx.com.crowdgine.util.math.Vector3f;

public class TPCamera extends GeneralCamera {
	//private float mouseSensitivity = 0.05f;
	private float movementSpeed = 10.0f; // move 10 units per second
	// time difference between frames (this probably shouldn't be here)
	private float dt = 0.01f;
	private Vector3f relativePosition;
	private boolean isAttached = false;
	
	public void init(Component component) {
		initKeyListeners(component);
	}

	private void initKeyListeners(Component component) {
		//component.addKeyListener(new MyKeyListener());
	}

	public TPCamera(Vector3f position) {
		this.position = position;
		this.entity = null;
		isAttached = false;
	}

	public TPCamera(Entity entity, Vector3f relativePosition) {
		/*Vector3f entPos = entity.getPosition();
		this.position = new Vector3f(entPos.getX() + position.getX(),
				entPos.getY() + position.getY(), entPos.getZ()
						+ position.getZ());
		*/
		this.entity = entity;
		this.relativePosition = relativePosition;
		isAttached = true;
	}

	/*
	public void setMouseSensitivity(float mouseSensitivity) {
		this.mouseSensitivity = mouseSensitivity;
	}*/

	public void setMovementSpeed(float movementSpeed) {
		this.movementSpeed = movementSpeed;
	}

	public void setDt(float dt) {
		this.dt = dt;
	}

	// increment the camera's current yaw rotation
	public void yaw(float amount) {
		// increment the yaw by the amount param
		yaw -= amount;
	}

	// increment the camera's current yaw rotation
	public void pitch(float amount) {
		// increment the pitch by the amount param
		pitch -= amount;
	}

	// moves the camera forward relitive to its current rotation (yaw)
	public void walkForward(float distance) {
		position.setX(position.getX() + (distance * movementSpeed)
				* (float) Math.sin(Math.toRadians(yaw)));
		position.setZ(position.getZ() - (distance * movementSpeed)
				* (float) Math.cos(Math.toRadians(yaw)));
	}

	// moves the camera backward relitive to its current rotation (yaw)
	public void walkBackwards(float distance) {
		position.setX(position.getX() - (distance * movementSpeed)
				* (float) Math.sin(Math.toRadians(yaw)));
		position.setZ(position.getZ() + (distance * movementSpeed)
				* (float) Math.cos(Math.toRadians(yaw)));
	}

	// strafes the camera left relitive to its current rotation (yaw)
	public void strafeLeft(float distance) {
		position.setX(position.getX() + (distance * movementSpeed)
				* (float) Math.sin(Math.toRadians(yaw - 90)));
		position.setZ(position.getZ() - (distance * movementSpeed)
				* (float) Math.cos(Math.toRadians(yaw - 90)));
	}

	// strafes the camera right relitive to its current rotation (yaw)
	public void strafeRight(float distance) {
		position.setX(position.getX() + (distance * movementSpeed)
				* (float) Math.sin(Math.toRadians(yaw + 90)));
		position.setZ(position.getZ() - (distance * movementSpeed)
				* (float) Math.cos(Math.toRadians(yaw + 90)));
	}

	public void interact(GL2 gl) {
		if (isAttached) {
			position = entity.getPosition().add(relativePosition);
		}
		super.interact(gl);
	}
/*
	// This is a temporary class for testing purposes, key listeners should be
	// defined in each game
	private class MyKeyListener extends KeyAdapter {
		public void keyPressed(KeyEvent ke) {
			int k = ke.getKeyCode();
			if (k == KeyEvent.VK_Z) {
				if (isAttached) {
					deattachFromEntity();
				} else {
					attachToEntity(entity);
				}
			}
			if (k == KeyEvent.VK_W)// move forward
			{
				if (!isAttached) {
					walkForward(dt);
				} else {
					entity.translate(new Vector3f(0, 0, -1));
				}
			}
			if (k == KeyEvent.VK_S)// move backwards
			{
				if (!isAttached) {
					walkBackwards(dt);
				} else {
					entity.translate(new Vector3f(0, 0, 1));
				}
			}
			if (k == KeyEvent.VK_A)// strafe left
			{
				if (!isAttached) {
					strafeLeft(dt);
				} else {
					entity.translate(new Vector3f(-1, 0, 0));
				}
			}
			if (k == KeyEvent.VK_D)// strafe right
			{
				if (!isAttached) {
					strafeRight(dt);
				} else {
					entity.translate(new Vector3f(1, 0, 0));
				}
			}
		}
	}
*/
}
